package com.littlec.sdk.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class NetworkUtil {

	public static boolean isCmwap(Context context) {
		int wifiStatus = getWifiStatus(context);
		if ((wifiStatus == -1) || (wifiStatus == 0)) {
			ConnectivityManager conManager = (ConnectivityManager) context
					.getSystemService("connectivity");
			NetworkInfo ni = conManager.getActiveNetworkInfo();
			if (ni != null) {
				String apn = ni.getExtraInfo();
				if ((apn != null) && (apn.equalsIgnoreCase("cmwap"))) {
					return true;
				}
			}
		}
		return false;
	}

	public static int getWifiStatus(Context context) {
		WifiManager wifi = (WifiManager) context.getSystemService("wifi");
		if (wifi.isWifiEnabled()) {
			ConnectivityManager connect = (ConnectivityManager) context
					.getSystemService("connectivity");
			NetworkInfo.State state = connect.getNetworkInfo(1).getState();
			if (state == NetworkInfo.State.CONNECTED) {
				WifiInfo info = wifi.getConnectionInfo();
				if ((info != null)
						&& (info.getSSID() != null)
						&& ((info.getSSID().equals("CMCC")) || (info.getSSID()
								.equals("CMCC-EDU")))) {
					return 2;
				}

				return 1;
			}

			return 0;
		}

		return -1;
	}

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService("connectivity");
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni != null) {
			return ni.getState() == NetworkInfo.State.CONNECTED;
		}
		return false;
	}
}
